//
//  TDMainContreller.m
//  liveDemo
//
//  Created by tuandai on 2017/4/10.
//  Copyright © 2017年 tuandai. All rights reserved.
//

#import "TDMainContreller.h"
#import "TDAnchorController.h"
#import "TDAudienceController.h"
#import "TCTLSPlatform.h"
#import "TCIMPlatform.h"
#import "TCLoginParam.h"

@interface TDMainContreller ()
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (nonatomic, strong) TCLoginParam *loginParam;
@end

typedef void (^TLSSucc)(TLSUserInfo *userInfo);

@implementation TDMainContreller

- (void)viewDidLoad {
    [super viewDidLoad];
    // 打印SDK的版本信息
    TDLog(@"SDK Version = %@", [[TXLivePush getSDKVersion] componentsJoinedByString:@"."]);
    
    self.accountTextField.text = @"q378789308";
    self.pwdTextField.text = @"123456";
    if (iPhone6Plus) {
        self.accountTextField.text = @"a2117238";
        
    }else if (iPhone6) {
        self.accountTextField.text = @"q378789308";
    }else if (isiPhone5) {
        self.accountTextField.text = @"37878930a";
    }
    
    // 测试暂时这么写
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 初始化IMSDK
        [[TCIMPlatform sharedInstance] initIMSDK];
    });
    
}


- (IBAction)anchorBtnClick:(id)sender {
    [SVProgressHUD showWithStatus:@"登录中..."];
    [self login:^(BOOL isLogin) {
        if (isLogin) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                TDAnchorController *vc = [TDAnchorController new];
                [self.navigationController pushViewController:vc animated:YES];
                [SVProgressHUD dismiss];
            });
            
        }else {
            
        }
    }];
}


- (IBAction)audienceBtnClick:(id)sender {
    [SVProgressHUD showWithStatus:@"登录中"];
    [self login:^(BOOL isLogin) {
        if (isLogin) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                TDAudienceController *vc = [TDAudienceController new];
                [self.navigationController pushViewController:vc animated:YES];
                [SVProgressHUD dismiss];
            });
            
        }else {
            [SVProgressHUD dismiss];
        }
    }];
    
}

#pragma mark - 登录TLS
-(void)login:(void(^)(BOOL isLogin))block {
    @weakify(self)
    int ret = [[TCTLSPlatform sharedInstance] pwdLogin:self.accountTextField.text andPassword:self.pwdTextField.text succ:^(TLSUserInfo *userInfo) {
        
        [weak_self loginIMSDK:userInfo];
        
        block(YES);
        
    } fail:^(TLSErrInfo *errInfo) {
        
        
        TDLog(@"\n 登录TLS失败-----%s %d %@", __func__, errInfo.dwErrorCode, errInfo.sExtraMsg);
        
        block(NO);
    }];
    
    if (ret != 0) {
        [SVProgressHUD showErrorWithStatus:@"内部错误"];
    }
}

#pragma mark - IM
- (void)loginIMSDK:(TLSUserInfo*)userInfo {
    if (!userInfo) {
        return;
    }
    self.loginParam.identifier = userInfo.identifier;
    self.loginParam.userSig = [[TLSHelper getInstance] getTLSUserSig:userInfo.identifier];
    self.loginParam.tokenTime = [[NSDate date] timeIntervalSince1970];
    [[TCIMPlatform sharedInstance] login:self.loginParam succ:^{
        // 持久化param
        [self.loginParam saveToLocal];
        // 进入主界面
        //        [[AppDelegate sharedAppDelegate] enterMainUI];
        
    } fail:^(int code, NSString *msg) {
        TDLog(@"\nIM登录失败%d----%@",code,msg);
        //        [weakSelf pullLoginUI];
    }];
}


-(TCLoginParam *)loginParam {
    if (!_loginParam) {
        BOOL isAutoLogin = [TCIMPlatform isAutoLogin];
        if (isAutoLogin) {
            _loginParam = [TCLoginParam loadFromLocal];
        }
        else {
            _loginParam = [[TCLoginParam alloc] init];
        }
    }
    return _loginParam;
}


@end
